<template>
  <div>
    <div class="top">
      <div style="float: right;margin-right: 10px;">
        <el-input
          v-model="kwInput"
          placeholder="输入邮件地址或者关键词进行筛选"
          class="input-with-select"
          clearable
          @keyup.native.enter="handleSearch"
        >
          <el-button slot="append" icon="el-icon-search" @click="handleSearch">搜索</el-button>
        </el-input>
      </div>
      <div style="float: right;margin-right: 10px;">
        <el-select v-model="state" placeholder="状态" @change="handleSearch">
          <el-option label="发送失败" value="false" />
          <el-option label="发送成功" value="success" />
        </el-select>
      </div>

      <div style="clear: both" />
    </div>

    <el-table
      v-loading.fullscreen.lock="listLoading"
      :data="data"
      border
      fit
      highlight-current-row
      style="width: 100%"
      @sort-change="handleSort"
    >
      <el-table-column align="center" width="300" prop="account" label="account" sortable="custom" />
      <el-table-column align="center" width="300" prop="sender_email" label="from" sortable="custom" />
      <el-table-column align="center" width="300" prop="receiver_email" label="to" sortable="custom" />
      <el-table-column align="center" label="subject" prop="subject" sortable="custom">
        <template slot-scope="scope">
          <span>{{ scope.row.subject }}</span>
          <el-popover
            placement="right"
            width="640"
            trigger="hover"
          >
            <div>
              <h2>{{ scope.row.subject }}</h2>
              <el-tag>from:{{ scope.row.account }}</el-tag>
              <el-tag>To:{{ scope.row.receiver_email }}</el-tag>
              <div style="margin-top: 10px" v-html="scope.row.body" />
            </div>
            <i slot="reference" style="float: right" class="el-icon-view" title="查看详情" />
          </el-popover>
          <!--          <span @click="handleDrawer(scope.row)">{{scope.row.subject}}</span>-->
        </template>
      </el-table-column>
      <el-table-column align="center" width="100" prop="success" label="是否成功">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="点击调整状态" placement="bottom-start">
            <el-switch v-model="scope.row.success" @change="handleModify(scope.row)" />
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column align="center" width="120" prop="create_at" label="发送时间" />
      <el-table-column align="center" label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="success" @click="handleAction('edit', scope.row)">修改</el-button>
          <el-button size="mini" type="danger" @click="handleAction('delete', scope.row)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>
    <el-drawer
      title="邮件内容"
      :wrapper-closable="false"
      append-to-body
      :visible.sync="drawer"
      size="50%"
    >
      <div>
        <h2>{{ currentrow.subject }}</h2>
        <el-tag>from:{{ currentrow.account }}</el-tag>
        <el-tag>To:{{ currentrow.receiver_email }}</el-tag>
        <div style="margin-top: 10px" v-html="currentrow.body" />
      </div>
    </el-drawer>
    <el-pagination
      v-if="data.length"
      background
      class="mt-pagination"
      :current-page="page"
      :page-sizes="[10,20,50,100]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handlePageChange"
    />

    <el-dialog
      append-to-body
      :title="temp.subject"
      :width="width"
      :visible.sync="innerDialogVisible"
      :close-on-click-modal="false"
    >
      <el-form :model="temp" label-position="left" label-width="120px">

        <el-form-item v-if="action !== 'delete'" label="邮箱账号">
          <el-input v-model="temp.subject" />
        </el-form-item>

        <el-form-item v-if="action !== 'delete'" label="邮件内容">
          <div v-html="temp.body" />
        </el-form-item>
        <el-form-item v-if="action !== 'delete'" label="account">
          <el-input v-model="temp.account" />
        </el-form-item>
        <el-form-item v-if="action !== 'delete'" label="from">
          <el-input v-model="temp.account" />
        </el-form-item>
        <el-form-item v-if="action !== 'delete'" label="to">
          <el-input v-model="temp.receiver_email" />
        </el-form-item>
        <el-form-item v-if="action !== 'delete'" label="server_type">
          <el-input v-model="temp.server_type" />
        </el-form-item>
        <el-form-item v-if="action !== 'delete'" label="account">
          <el-input v-model="temp.account" />
        </el-form-item>
        <el-form-item v-if="action !== 'delete'" label="password">
          <el-input v-model="temp.password" />
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="innerDialogVisible = false">取 消</el-button>
        <el-button :type="action !== 'delete'?'primary':'danger'" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
    <orderinfo :keyword="orderinfokeyword" :orderinfo-visible="orderinfoVisible" @closeDialog="orderinfoVisible=false" />
  </div>
</template>

<script>
import { Get, Post, Put, Delete } from '@/api/eco/emailreply'
import qs from 'qs'

const orderMapper = {
  'ascending': 0,
  'descending': 1,
  'null': ''
}
export default {
  name: 'Emailreply',
  data() {
    return {
      drawer: false,
      data: [],
      listLoading: false,
      kwInput: '',
      state: 'false',
      order_by: 'create_at',
      order_desc: 1,
      // pagination
      pagesize: 20,
      page: 1,
      total: 0,
      currentrow: {
        subject: '',
        body: '',
        mail_from: '',
        mail_to: ''
      },
      action: null,
      dialogVisible: false,
      title: '',
      temp: {
        subject: '',
        mail_to: '',
        mail_from: '',
        body: '',
        id: ''
      },
      width: '',
      projects: [],
      innerDialogVisible: false
    }
  },
  watch: {
    webVisible(v) {
      this.dialogVisible = v
      if (v) {
        this.getList()
      }
    }
  },
  created() {
    this.handleSearch()
  },
  methods: {
    handleDrawer(row) {
      this.currentrow = row
      this.drawer = true
    },
    resetTemp() {
      this.temp = {
        account: '',
        password: 'Euc@16IumGg',
        smtp_host: 'smtp.zoho.com',
        smtp_port: '465',
        project_id: ''
      }
    },
    getList() {
      this.listLoading = true
      Get(null, qs.stringify({
        kw: this.kwInput,
        state: this.state,
        pagesize: this.pagesize,
        page: this.page,
        order_by: this.order_by,
        order_desc: this.order_desc
      }))
        .then(response => {
          this.scrollTop()
          this.data = response.data.data
          this.listLoading = false
          this.total = response.data.total
        }).catch(err => {
          console.log(err)
          this.listLoading = false
        })
    },
    handleModify(row) {
      Put(row.id, {
        id: row.id,
        success: row.success
      }).then((response) => {
        this.notify('更新成功')
      })
    },
    handleAction(action, row) {
      this.action = action
      switch (action) {
        case 'create':
          this.width = '50%'
          this.title = row
          this.resetTemp()
          this.innerDialogVisible = true
          break
        case 'delete':
          this.width = '30%'
          this.title = `确认要删除${row.subject}?`
          this.temp = Object.assign({}, row)
          this.innerDialogVisible = true
          break
        case 'edit':
          this.width = '50%'
          this.title = `修改站点${row.subject}`
          this.temp = Object.assign({}, row)
          this.innerDialogVisible = true
          break
      }
    },
    confirm() {
      const _data = this.temp
      switch (this.action) {
        case 'create':
          Post(_data
          ).then(response => {
            this.innerDialogVisible = false
            this.getList()
            this.$emit('reload')
            this.notify('添加成功')
          })
          break
        case
          'delete'
          :
          Delete(_data.id).then((response) => {
            this.innerDialogVisible = false
            this.getList()
            this.$emit('reload')
            this.notify('删除成功')
          })
          break
        case
          'edit'
          :
          Put(_data.id, _data).then((response) => {
            this.innerDialogVisible = false
            this.$emit('reload')
            this.getList()
            this.notify('更新成功')
          })
          break
      }
    },
    notify(message, duration) {
      this.$notify({
        title: '成功',
        message: message,
        type: 'success',
        duration: duration || 2000
      })
    },
    // pageSize 改变后触发 table 更新
    handleSizeChange(val) {
      this.pagesize = val
      this.getList()
    },
    // page 改变后触发 table 更新
    handlePageChange(val) {
      this.page = val
      this.getList()
    },
    // 点击搜索
    handleSearch() {
      this.page = 1
      this.getList()
    },
    handleSort(v) {
      this.order_by = v.prop
      this.order_desc = orderMapper[v.order]
      this.getList()
    },
    scrollTop() {
      window.scrollTo(0, 0)
    },
    close() {
      this.$emit('closeWebDialog', false)
    }
  }
}
</script>
